package com.vtc.session14;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;

import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;

public class NetworkRequest {

	public static String doRequest(List<NameValuePair> data,String url){
		try {
		    // step 1: enable netwowrk connection
		    StrictMode.setThreadPolicy(ThreadPolicy.LAX);
     		// step 2: create object connect to target url
	     	HttpClient httpClient = new DefaultHttpClient();
		    HttpPost httpPost = new HttpPost(url);
		
     		if(data!=null) httpPost.setEntity(new UrlEncodedFormEntity(data));
	    	// step 3: send data
	        ResponseHandler<String> httpResponse = new BasicResponseHandler();
		    String dataResponse = httpClient.execute(httpPost,httpResponse);
		    return dataResponse;
		
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
}
